package com.cinema;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class Guestbook extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1252081483344070408L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		String guestbookName = req.getParameter("guestbookName");
		String content = req.getParameter("content");
		Date date = new Date();
		
		Key guestbookKey = KeyFactory.createKey("GuestBook", guestbookName);
		Entity greeting = new Entity("Greeting", guestbookKey);
		greeting.setProperty("User", user);
		greeting.setProperty("Date", date);
		greeting.setProperty("Content", content);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(greeting);
		
		resp.sendRedirect("guestbook.jsp?guestbookName=" + guestbookName);
	}
}
